﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.STMSApp
{
    public static class FormUtility//static 静态类
                                   //扩展方法前提条件：必须是静态类中的静态方法
    {
       
        public static void AddPanelForm(this Panel panel,Form frm)  //静态方法
        {
            bool bExist = true;
            //检查Form是否已打开
            if (CheckOpenForm(frm.Name))
            {
                Form f=GetOpenFrom(frm.Name);
                //判断当前显示的form是否已经显示在panel中
                bool bl = false;
                foreach (Control c in panel.Controls)
                {
                    if (c is Form)
                    {
                        Form form = c as Form;
                        if (form.Name==f.Name)
                        {
                            frm = form;
                            bl = true;
                             bExist = false;
                            break;
                        }
                    }
                }//ctrl+k+d 格式化显示
                if (!bl)
                {
                    frm = f;
                }
            }


            if (!bExist)
            {
                panel.Controls.Clear();
                frm.TopLevel = false;
                frm.WindowState = FormWindowState.Maximized;
                frm.FormBorderStyle = FormBorderStyle.None;
                frm.Dock = DockStyle.Fill;
                panel.AutoScroll = true;
                panel.Controls.Add(frm);
                frm.Show();
            }
            
        }

        public static bool CheckOpenForm(string frmName)
        {
            bool bl=false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name==frmName)
                {
                    bl = true;
                    break;
                }
            }
            return bl;
        }

        public static Form GetOpenFrom(string frmName)
        {
            Form frm=null;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name==frmName)
                {
                    frm=f;
                    break;
                }
            }
            return frm;
        } 
    }
}
